package com.ys.web.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.criterion.Conjunction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ys.WebConstants;
import com.ys.web.dao.CommonDao;
import com.ys.web.entity.CustomerFollowUp;
import com.ys.web.entity.OriginBaidu;
import com.ys.web.entity.User;
import com.ys.web.service.FollowUpService;

/**
 * 
 * @author xiezhengliang
 *
 */
@Service("FollowUpService")
@Transactional
public class FollowUpServiceImpl implements FollowUpService {

	@Autowired
	private CommonDao commonDao;
	
	@Resource(name = "jdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public List<CustomerFollowUp> getcustomerFollowUp(Integer companyId,Integer page,Integer rows) {
		DetachedCriteria dc = DetachedCriteria.forClass(CustomerFollowUp.class);
		List<CustomerFollowUp> pageList =null;
		Conjunction conjunction = Restrictions.conjunction();
		if(page!=null&&rows!=null){
			conjunction.add(Restrictions.eq("companyId", companyId));
			dc.add(conjunction);
			pageList = commonDao.pageList(dc, (page-1)*rows, rows);
		}else{
			conjunction.add(Restrictions.eq("companyId", companyId));
			dc.addOrder(Order.desc("followUpTime"));
			dc.add(conjunction);
			pageList = commonDao.pageList(dc, 0, WebConstants.MAX_REQUEST_ROW_EACH);
		}
//		String hql = "from CustomerFollowUp where companyId=? order by followUpTime desc";
//		List<CustomerFollowUp> CustomerFollowUp = commonDao.findHql(hql,companyId);
		return pageList;
	}

	@Override
	public int addFollowUp(String followUpTitle,String followUpContent,String operateUsername,int operateUserId,int companyId){
		String hql = "insert into  customer_follow_up(follow_up_title,follow_up_content,user_id,user_name,company_id,state,follow_up_time) values (?,?,?,?,?,?,now())";
		Integer backResult = commonDao.executeSql(hql, followUpTitle, followUpContent,operateUserId,operateUsername,companyId,1);
		return backResult;
	}

	@Override
	public List<User> getEmployeeName(Integer operateUserId) {
//		String hql = "from User where id=?";
//		List<User> user = commonDao.findHql(hql,operateUserId);
		DetachedCriteria dc = DetachedCriteria.forClass(User.class);
		List<User> user =null;
		Conjunction conjunction = Restrictions.conjunction();
		conjunction.add(Restrictions.eq("id", operateUserId));
		dc.add(conjunction);
		//只取第一个
		user = commonDao.pageList(dc, 0,1);
		return user;
	}

	@Override
	public List<CustomerFollowUp> getFollowUpDetail(int followUpId) {
//		String hql = "from CustomerFollowUp where id=?";
//		List<CustomerFollowUp> CustomerFollowUp = commonDao.findHql(hql,followUpId);
		DetachedCriteria dc = DetachedCriteria.forClass(CustomerFollowUp.class);
		List<CustomerFollowUp> pageList =null;
		Conjunction conjunction = Restrictions.conjunction();
		conjunction.add(Restrictions.eq("id", followUpId));
		dc.add(conjunction);
		pageList = commonDao.pageList(dc, 0, WebConstants.MAX_REQUEST_ROW_EACH);
		return pageList;
	}

	@SuppressWarnings("deprecation")
	@Override
	public int countNum(int companyId) {
		String sql = "select count(*) from customer_follow_up where company_id="+companyId;
		int countNum = jdbcTemplate.queryForInt(sql);
		return countNum;
	}

}
